﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Outdoors.Helpers.Icons;
using Windows.Devices.Geolocation;
using System.Device.Location;
using Microsoft.Phone.Maps.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using Outdoors.ViewModels;
using System.Windows.Media.Imaging;

namespace Outdoors
{
    public partial class DetailsPage : PhoneApplicationPage
    {
        private double Latitude { get; set; }
        private double Longitude { get; set; }
        public DetailsPage()
        {
            InitializeComponent();

            Loaded += DetailsPage_Loaded;

            BuildLocalizedApplicationBar();
        }

        private void DetailsPage_Loaded(object sender, RoutedEventArgs e)
        {
            DataContext = new ZoomViewModel();

            UpdateMap();
        }
        private void UpdateMap()
        {
            //Geolocator geolocator = new Geolocator();
            //geolocator.DesiredAccuracyInMeters = 50;

            //Geoposition geoposition = await geolocator.GetGeopositionAsync(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(30));
            //var gpsCoordinateCenter = new GeoCoordinate(geoposition.Coordinate.Latitude, geoposition.Coordinate.Longitude);

            outdoorsMap.Center = new GeoCoordinate(Latitude, Longitude);
            outdoorsMap.ZoomLevel = 17;

            Image myImage3 = new Image();
            BitmapImage bi3 = new BitmapImage();
            bi3.UriSource = new Uri("/Assets/redpin.png", UriKind.Relative);
            myImage3.Stretch = Stretch.Fill;
            myImage3.Source = bi3;
            MapOverlay overlay2 = new MapOverlay
            {
                GeoCoordinate = outdoorsMap.Center,
                Content = new Image
                {
                    Source = bi3
                }
            };

            //MapOverlay overlay = new MapOverlay
            //               {
            //                   GeoCoordinate = outdoorsMap.Center,
            //                   Content = new Ellipse
            //                                 {
            //                                     Fill = new SolidColorBrush(Colors.Red),
            //                                     Width = 20,
            //                                     Height = 20
            //                                 }
            //               };
            MapLayer layer = new MapLayer();
            //layer.Add(overlay);
            layer.Add(overlay2);
            outdoorsMap.Layers.Add(layer);

        }

        private void BuildLocalizedApplicationBar()
        {
            ApplicationBar = new ApplicationBar();

            ApplicationBarIconButton appBarFlickrButton = new ApplicationBarIconButton(new Uri(Icons.FlickrSearch, UriKind.Relative));
            appBarFlickrButton.Text = "Search Flickr";
            appBarFlickrButton.Click += SearchClick;
            ApplicationBar.Buttons.Add(appBarFlickrButton);

            //ApplicationBarIconButton appBarFoursquareButton = new ApplicationBarIconButton(new Uri(Icons.FoursquareSearch, UriKind.Relative));
            //appBarFoursquareButton.Text = "Search Foursquare";
            //appBarFoursquareButton.Click += SearchClick;
            //ApplicationBar.Buttons.Add(appBarFoursquareButton);
        }

        private void SearchClick(object sender, EventArgs e)
        {
            string navigateTo = string.Format("/FlickrSearchResults.xaml?latitude={0}&longitude={1}",
                                outdoorsMap.Center.Latitude, outdoorsMap.Center.Longitude);

            NavigationService.Navigate(new Uri(navigateTo, UriKind.RelativeOrAbsolute));
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            Latitude = Convert.ToDouble(NavigationContext.QueryString["latitude"]);
            Longitude = Convert.ToDouble(NavigationContext.QueryString["longitude"]);

            textPageName.Text = NavigationContext.QueryString["name"];
        }

        private void ButtonZoomIn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                outdoorsMap.ZoomLevel++;
            }
            catch (Exception)
            { }
        }

        private void ButtonZoomOut_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                outdoorsMap.ZoomLevel--;
            }
            catch (Exception)
            { }
        }
    }
}